//https://leetcode.cn/problems/construct-string-from-binary-tree/

class Solution {
public:
    string tree2str(TreeNode* root) {
        string ret;
        if (root == nullptr)return "";

        ret += to_string(root->val);

        if (root->left || root->right)
        {
            ret += "(";
            ret += tree2str(root->left);
            ret += ")";
        }

        if (root->right)
        {
            ret += "(";
            ret += tree2str(root->right);
            ret += ")";
        }

        return ret;

    }
};